package org.smartagents.body;

import org.smartagents.enviro.EnvironmentConstants;
import org.smartagents.global.Identifiable;
import org.smartagents.global.Trigerable;

import java.util.ArrayList;
import java.util.List;

public class Body extends Identifiable {

    private Brain brain = new Brain();

    private List<Substance> substances = new ArrayList<Substance>();

    private int rotation = EnvironmentConstants.EAST;

    private StringBuffer buf;


    public Body() {
        setMaxTick(1);
    }

    public List<Substance> getSubstances() {
        return substances;
    }


    public String identify() {
        StringBuilder sb = new StringBuilder();

        for (Identifiable identifiable : substances) {
            sb.append(identifiable.identify());
        }

        return sb.toString();
    }

    public void trigger(String impulse) {

    }

    ;

    public void tick() {
        for (Substance substance : substances) {
            substance.doTick();
        }
    }

    public Neurone getCortex() {
        return brain.getCortex();
    }

    public void setCortex(Neurone neu) {
        brain.setCortex(neu);
        for (Trigerable subs : substances) {
            neu.addLink(subs);
        }
    }

    public int getRotation() {
        return rotation;
    }

    public void setRotation(int rotation) {
        this.rotation = rotation;
        if (rotation > 4) {
            this.rotation = 1;
        }
        if (rotation < 1) {
            this.rotation = 4;
        }

    }

    @Override
    public String toString() {
        if (buf == null) {
            buf = new StringBuffer();
            for (Substance sub : substances) {
                buf.append(sub.toString());
            }
        }

        return buf.toString();
    }
}
